package com.xuan.core.controller.tools;

import com.nlf.calendar.EightChar;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import com.xuan.core.configuration.result.ResponseResult;
import com.xuan.core.configuration.result.ResponseResultEnum;
import com.xuan.core.dto.tools.ToolsDto;
import com.xuan.utils.common.CommonUtil;
import com.xuan.utils.common.DateUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工具
 */
@RestController
@RequestMapping("/tools")
public class ToolsController {

    /**
     * 获取指定日期的干支
     *
     * @param dto 工具
     * @return 指定日期的干支
     */
    @PostMapping("/ganZhi")
    @Cacheable(value = "ganZhi", key = "'_' + #dto.date.year + #dto.date.month + #dto.date.date + #dto.date.hours")
    public ResponseResult<List<String>> ganZhi(@RequestBody ToolsDto dto) {

        // 获取当前日期的干支
        List<String> nowGanZhi = CommonUtil.getGanZhi(dto);
        return ResponseResult.result(ResponseResultEnum.GET_SUCCESS, nowGanZhi);

    }

    /**
     * 通过干支获取日期
     *
     * @param dto 工具
     * @return 日期
     */
    @PostMapping("/ganZhiToDate")
    @Cacheable(
            value = "ganZhiToDate",
            key = "'_' + #dto.beginYear + #dto.yearGanZhi + #dto.monthGanZhi + #dto.dayGanZhi +  #dto.hourGanZhi + " +
                    "#dto.yearGanZhiSet + #dto.monthGanZhiSet + #dto.dayGanZhiSet + #dto.hourGanZhiSet"
    )
    public ResponseResult<List<Map<String, Object>>> ganZhiToDate(@RequestBody ToolsDto dto) {

        // 干支转换日期
        List<Map<String, Object>> ganZhiToDate = CommonUtil.ganZhiToDate(dto);
        return ResponseResult.result(ResponseResultEnum.GET_SUCCESS, ganZhiToDate);

    }

    /**
     * 获取当前农历日期、日柱时柱、十二时辰
     */
    @PostMapping("/nowLunarDate")
    public ResponseResult<List<Object>> nowLunarDate() {

        // 1、使用当前日期初始化
        Lunar lunar = new Lunar();

        List<Object> list = new ArrayList<>();

        // 2、设置农历日期
        list.add(CommonUtil.getLunarStr(lunar)); // 获取农历日期字符串

        // 3、获取当前日柱时柱
        EightChar ec = lunar.getEightChar();
        String nowDayHourGanZhi = ec.getDay() + "日" + ec.getTime() + "时"; // 当前日柱时柱
        list.add(nowDayHourGanZhi);

        // 4、设置十二时辰
        int hour = 0; // 小时数
        List<String> twelveHours = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Solar solar = new Solar(); // 公历日期
            Date date = DateUtil.timeToDate(solar.getYear(), solar.getMonth(), solar.getDay(), hour, solar.getMinute(), solar.getSecond());
            // 4.1、通过公历初始化
            lunar = new Lunar(date);
            ec = lunar.getEightChar();
            String dayHourGanZhi = ec.getDay() + "日" + ec.getTime() + "时"; // 日干支时干支
            twelveHours.add(dayHourGanZhi);
            hour += 2;
        }
        list.add(twelveHours);

        // 5、返回数据
        return ResponseResult.result(ResponseResultEnum.SUCCESS, list);

    }


}
